﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Baopx_API;

namespace A.Controller
{
    public class BackEndMenuController
    {
        private static List<BackEndMenu> _backEndMenus = A.M.d.BackEndMenus.ToList();

        private static void UpdateCache()
        {
            _backEndMenus = A.M.d.BackEndMenus.ToList();
        }

        public static List<BackEndMenu> GetAllBackEndMenuParent()
        {
            return _backEndMenus.Where(i => i.ParentId == null && i.IsActive).ToList();
        }

        public static List<BackEndMenu> GetAllBackEndMenuChildByParentId(object parentId)
        {
            return _backEndMenus.Where(i => i.ParentId == Convert.ToInt32(parentId) && i.IsActive).ToList();
        }

        public static List<BackEndMenu> GetAllBackEndMenuChild()
        {
            return _backEndMenus.Where(i => i.ParentId !=null).ToList();
        }

        public static void Insert(BackEndMenu backEndMenu)
        {
            A.M.d.BackEndMenus.InsertOnSubmit(backEndMenu);
            A.M.d.SubmitChanges();
            UpdateCache();
        }

        public static void Update()
        {
            A.M.d.SubmitChanges();
            UpdateCache();
        }

        public static BackEndMenu GetBackEndMenuById(object id)
        {
            return _backEndMenus.SingleOrDefault(i => i.Id == Convert.ToInt32(id));
        }

        public static void Delete(BackEndMenu backEndMenuDelete)
        {
            A.M.d.BackEndMenus.DeleteOnSubmit(backEndMenuDelete);
            A.M.d.SubmitChanges();
            UpdateCache();
        }
    }
}
